package com.smart.nine.spel.sample;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * 测试类
 *
 * @author junrui.wang
 * @create 2019/3/18
 */
public class ScriptSample {
    public static void main(String[] args) throws ScriptException, NoSuchMethodException {
        //创建脚本引擎管理器
        ScriptEngineManager manager = new ScriptEngineManager();
        //创建脚本引擎
        ScriptEngine scriptEngine = manager.getEngineByName("JavaScript");
        String scriptText = "function sum(a,b) {return a+b}";
        //注册Script函数脚本
        scriptEngine.eval(scriptText);
        Invocable invocable = (Invocable) scriptEngine;
        //使用Invocable调用注册的函数脚本
        Object result = invocable.invokeFunction("sum", 100, 99);
        System.out.println(result);
    }
}
